#include "../../common/icsearch.hpp"
#include <gflags/gflags.h>
#include<gtest/gtest.h>

DEFINE_bool(run_mode, false, "程序运行模式 --false: 调试, --true: 发布");
DEFINE_string(log_file, "", "发布模式下, 指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 指定日志的输出等级");
std::shared_ptr<elasticlient::Client> client;
TEST(创建索引测试,test1)
{
    // 索引创建
    EsIndex index(client, "test_user");
    //!bug
    bool ret = index.append("nickname").append("phone", "keyword", "standard", true).create();
    ASSERT_TRUE(ret);  
}
TEST(数据新增测试,test1)
{
     bool ret = ESinsert(client, "test_user")\
                .append("nickname", "zhangsan").\
                append("phone", "1234567890").\
                insert("00001");
    ASSERT_TRUE(ret); 
    ret = ESinsert(client, "test_user")\
                .append("nickname", "吴志强").\
                append("phone", "1763424636").\
                insert("00002");
    ASSERT_TRUE(ret); 
}
TEST(数据更新测试,test1)
{
    // 数据更新
    bool ret = ESinsert(client, "test_user").\
                 append("nickname", "zhangsan").\
                 append("phone", "0987654321").\
                    insert("00001");
   ASSERT_TRUE(ret); 
}

TEST(检索测试,test1)
{
     // 检索
    sleep(1);
    Json::Value user = ESserach(client, "test_user")
                           .append_shoule_match("phone.keyword", "1763424636")
                           .append_must_not_terms("nickname.text", {"zhangsan"})
                           .search();
    ASSERT_FALSE(user.empty());
    ASSERT_TRUE(user.isArray());

    int sz = user.size();
    for (int i = 0; i < sz; i++)
    {
        //!bug
        std::cout << user[i]["_source"]["nickname"].asCString() << std::endl;
        std::cout << user[i]["_source"]["phone"].asCString() << std::endl;
    }
    ASSERT_TRUE(ESremove(client, "test_user").remove("00001"));
}
int main(int argc, char *argv[])
{
    testing::InitGoogleTest();
    google::ParseCommandLineFlags(&argc, &argv, true);
    init(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    std::vector<std::string> host_list = {"http://127.0.0.1:9200/"};
    client = std::make_shared<elasticlient::Client>(host_list);
    return RUN_ALL_TESTS();
}